<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">通知管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>通知管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="notice-search-key">
                <option value="">-请选择-</option>
                <option value="nticid">ID</option>
                <option value="ntctle">标题</option>
                <option value="pbsher">发布人</option>
            </select>&emsp;
            <input id="notice-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="notice-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="notice-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>发布</button>
            <button id="notice-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
            <select id="user-notice" lay-filter="user-notice">
                <option value="">-请选择-</option>
                <option value="publish">发布的通知</option>
                <option value="received">收到的通知</option>
            </select>&emsp;
        </div>

        <table class="layui-table" id="notice-table" lay-filter="notice-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="notice-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="notice-tpl-state">
    <input type="checkbox" lay-filter="notice-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'index', 'config', 'admin', 'laydate'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var index = layui.index;
        var userinfo = '';

        index.getUser(function (user) {
            userinfo = user;
        })
        tableRender("publish");

        function tableRender(val) {
            console.log(val);
            // 渲染表格
            if (val == "publish") {
                table.render({
                    elem: '#notice-table',
                    // url: config.base_server + 'user_query.json',
                    url: config.base_server + 'api-user/notice/getSelfPublishNotice',
                    method: 'GET',
                    where: {
                        userid: userinfo.id,
                        access_token: config.getToken().access_token
                    },
                    page: true,
                    cols: [[
                        {type: 'numbers'},
                        {field: 'nticid', sort: true, title: '通知编号'},
                        {field: 'pbsher', sort: true, title: '发布人'},
                        {field: 'ntctle', sort: true, title: '通知标题'},
                        {field: 'contnt', sort: true, title: '内容'},
                        {field: 'clsdat', sort: true, title: '截止日期'},
                        {
                            sort: true, templet: function (d) {
                                return util.toDateString(d.createTime);
                            }, title: '创建时间'
                        },
                        {align: 'center', toolbar: '#notice-table-bar', title: '操作', width: 150}
                    ]]
                });
            } else if (val == "received") {
                table.render({
                    elem: '#notice-table',
                    // url: config.base_server + 'user_query.json',
                    url: config.base_server + 'api-user/notice/getReceivedAndMarkNoReadNotice',
                    method: 'GET',
                    where: {
                        userid: userinfo.id,
                        access_token: config.getToken().access_token
                    },
                    page: true,
                    cols: [[
                        {type: 'numbers'},
                        {field: 'nticid', sort: true, title: '通知编号'},
                        {field: 'pbsher', sort: true, title: '发布人'},
                        {field: 'ntctle', sort: true, title: '通知标题'},
                        {field: 'contnt', sort: true, title: '内容'},
                        {field: 'clsdat', sort: true, title: '截止日期'},
                        {
                            sort: true, templet: function (d) {
                                return util.toDateString(d.createTime);
                            }, title: '创建时间'
                        },
                        {field: 'isRead', sort: true, templet: '#notice-tpl-state', title: '状态', width: 150}
                    ]]
                });
            }
        }

        // 添加按钮点击事件
        $('#notice-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(notice-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-user/notice/deleteNotice?' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                });
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            //传递通知数据到修改界面
            admin.putTempData('t_notice', data);
            var title = data ? '修改通知' : '发布通知';
            admin.popupCenter({
                title: title,
                path: 'pages/notice/notice_form.html',
                area: '800px',
                offset: '50px',
                finish: function () {
                    table.reload('notice-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#notice-btn-search').click(function () {
            var key = $('#notice-search-key').val();
            var value = $('#notice-search-value').val();
            table.reload('notice-table', {where: {searchKey: key, searchValue: value}});
        });

        form.on('select(user-notice)', function (data) {
            tableRender(data.value)
        })
    });
</script>